<!DOCTYPE html>
<html>
<body>
<script src="../../resources/js-test.js"></script>

<div id="detector"></div>

<style>
    #detector { width: 100px; }
    @media (resolution: 1.00dppx) { #detector { width: 10px; } }
    @media (resolution: 1.50dppx) { #detector { width: 15px; } }
    @media (resolution: 2.00dppx) { #detector { width: 20px; } }
    @media (resolution: 2.25dppx) { #detector { width: 23px; } }
    @media print and (min-resolution: 3dppx) { #detector { width: 30px; } }
</style>

<script>
    description("CSS3 media query test: resolution query with dppx. Using style element, @media css rule.")

    if (window.testRunner && window.internals) {
        testRunner.dumpAsText();

        function resolutionFromStyle() {
            var width = getComputedStyle(document.getElementById("detector")).width;
            switch (width) {
                case "10px":
                    return 1;
                case "15px":
                    return 1.5;
                case "20px":
                    return 2;
                case "23px":
                    return 2.25;
                case "30px":
                    return 3;
                case "31px":
                    return 3.125;
                default:
                    return undefined;
            }
        }

        function appendMediaRule(rule) {
            var lastStyleSheet = document.styleSheets[document.styleSheets.length - 1];
            lastStyleSheet.insertRule(rule, lastStyleSheet.cssRules.length);
        }

        shouldBe("matchMedia('(resolution: 0dpi)').matches", "false");
        shouldBe("matchMedia('(min-resolution: 0dpi)').matches", "false");
        shouldBe("matchMedia('(max-resolution: 0dpi)').matches", "false");

        testRunner.setBackingScaleFactor(1.5, function() {});
        shouldBe("matchMedia('(resolution: 1.5dppx)').matches", "true");
        shouldBe("resolutionFromStyle()", "1.5");

        testRunner.setBackingScaleFactor(2, function() {});
        shouldBe("matchMedia('(resolution: 2dppx)').matches", "true");
        shouldBe("resolutionFromStyle()", "2");

        testRunner.setBackingScaleFactor(1, function() {});
        shouldBe("matchMedia('(resolution: 1dppx)').matches", "true");
        shouldBe("resolutionFromStyle()", "1");

        testRunner.setBackingScaleFactor(2.25, function() {});
        shouldBe("matchMedia('(resolution: 2.25dppx)').matches", "true");
        shouldBe("resolutionFromStyle()", "2.25");
        shouldBe("matchMedia('(resolution)').matches", "true");
        shouldBe("matchMedia('(resolution: 216dpi)').matches", "true");
        shouldBe("matchMedia('(min-resolution: 216dpi)').matches", "true");
        shouldBe("matchMedia('(max-resolution: 216dpi)').matches", "true");

        shouldBe("matchMedia('(resolution: 85dpcm)').matches", "true");
        shouldBe("matchMedia('(min-resolution: 85dpcm)').matches", "true");
        shouldBe("matchMedia('(max-resolution: 85dpcm)').matches", "true");

        // Test printing.

        internals.settings.setMediaTypeOverride("print");
        shouldBe("matchMedia('(min-resolution: 300dpi)').matches", "true");
        shouldBe("matchMedia('(min-resolution: 118dpcm)').matches", "true");

        // Should match the one requiring 'print' media type.
        shouldBe("resolutionFromStyle()", "3");

        // As well as those matching 'all'.
        appendMediaRule("@media (resolution: 3.125dppx) { #detector { width: 31px; } }");
        shouldBe("resolutionFromStyle()", "3.125");
    }
</script>

</body>
</html>
